"use strict";
/* Controllers */
app.controller("alertDashboardController", ['$scope', '$location', '$rootScope', '$routeParams', 'alertDashboardService', function ($scope, $location, $rootScope, $routeParams, alertDashboardService) {

    var vm = $scope.vm;
    vm.p = {};

   
    var errorFn = function (result) {
        alert(result);
    };

    $scope.listTopicDashboard = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        $('#list_table1').bootstrapTable('showLoading');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $('#list_table1').bootstrapTable('load', data);
            $('#list_table1').bootstrapTable('hideLoading');
        };
        alertDashboardService.listTopicDashboard(params, procFn, errorFn);
    };

    $scope.listRuleDashboard = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        $('#list_table2').bootstrapTable('showLoading');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $('#list_table2').bootstrapTable('load', data);
            $('#list_table2').bootstrapTable('hideLoading');

        };
        alertDashboardService.listRuleDashboard(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table1');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        var $resultTable = $('#list_table2');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        $scope.listTopicDashboard();
        $scope.listRuleDashboard();
    };

    $scope.searchAlertDashboard = function () {
        $('#list_table1').bootstrapTable('refresh');
        $('#list_table2').bootstrapTable('refresh');
        $scope.listTopicDashboard()
        $scope.listRuleDashboard()
    };
    $scope.toggleInteval = function (interval) {
        $scope.vm.p.interval = interval;
        $scope.searchAlertDashboard()
    }

    $scope.initializeController();

}]);

// https://hrg-luffy.58corp.com/#/alarm/rule/create?type=edit&topicId=1&id=99

/* Services */
app.service('alertDashboardService', ['ajaxService', function (ajaxService) {

    this.listTopicDashboard = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/alert/listTopicDashboard.do", successFunction, errorFunction);
    };

    this.listRuleDashboard = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/alert/listRuleDashboard.do", successFunction, errorFunction);
    };

}]);




function topicOptFormat(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/bcp/alert/alert_log_list/', value,'" target="_blank">', "<u>查看</u>" , '</a>'
    ].join('');
}


function ruleOptFormat(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/bcp/alert/alert_log_list/', value,'" target="_blank">', "<u>查看</u>" , '</a>'
    ].join('');
}